package engine.scene
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.flag.Funcid;
	import engine.flag.GameDefine;
	import engine.flag.ObjType;
	import engine.flag.PlayerActionFlag;
	import engine.logic.ActSee;
	import engine.logic.walk.Astar.ForbiddenAreaLogic;
	import engine.logic.walk.WalkManager;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.CampBattleLogic;
	import engine.newUi.DuplicatesLogic;
	import engine.newUi.FamilyDinnerUI;
	import engine.newUi.NoticeUI;
	import engine.newUi.SitCrossleggedLogic;
	import engine.newUi.TopOtherPlayerLogic;
	import engine.scene.player.BasePlayerModel;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	
	public class SceneClickLogic
	{
		private var _model  		:ModelLocator;
		
		private var _distanceMin 	:int;
		
		private var _distanceMax	:int 
		/**当前门点**/
		private var _tObjIndex		:Point;
		
		private static var _oldClickPoint:Point	=	new Point();
		public static var currentNPC:BasePlayerModel;
		public function SceneClickLogic()
		{
			_model			=	ModelLocator.getInstance();
			var distance : Object  =  StaticIni.getIniObj("Distance");
			if(distance)
			{
				_distanceMin = distance.min;
				_distanceMax = distance.max;
			}
		}
		
		/**
		 *	点击场景 
		 * @param rolePlayer	主角
		 * @param scenePlayer	点击的目标
		 * 
		 */
		public function sceneClick( rolePlayer : BasePlayerModel, scenePlayer : BasePlayerModel ):void
		{
			var objId : Point = scenePlayer.playerObj.ObjID;
			if(scenePlayer.playerObj.GetProperties("DoorType") == 3)
			{
				//				if(Point.distance(rolePlayer.point,scenePlayer.point)<=_distanceMax)
				//				{
				playerMove1( rolePlayer, scenePlayer, leaveGuild);
				//					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEAVE_GUILD_SCENE);
				return;
				//				}
			}
			switch( scenePlayer.playerObj.Type )
			{
				case ObjType.Player:					 
					//派发NPC被点击事件
					//					return;
					//					_model.dispatchEvent(new SceneEvent(SceneEvent.PlayerClick,[scenePlayer.playerName]));
					//					MenuListManager.clickPlayer( scenePlayer.playerName );
					TopOtherPlayerLogic.getInstance().callForShow(scenePlayer.playerName,scenePlayer.playerObj.GetPropertiesEx("ClotheModel"));
					break;
				case ObjType.ForbiddenBox:
				case ObjType.NPC:
					if(GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene)
					{
						SceneManager.getInstance().norSceneView.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
						/*SceneManager.getInstance().norSceneView.showWalkIcon(new Point(scenePlayer.x,scenePlayer.y));
						var cellSize:int	=	ForbiddenAreaLogic.getInstance()._cellSize;
						ForbiddenAreaLogic.getInstance().startFindPath(scenePlayer.x/cellSize,
						scenePlayer.y/cellSize,
						GameData.Role.x/cellSize,
						GameData.Role.y/cellSize,
						checkDis);
						
						checkDis();
						function checkDis():void
						{
						if(  Point.distance( GameData.Role.point, scenePlayer.point ) < 40 )
						{
						SystemSender.getInstance().CustomMsg(
						CustomToServerFlag.CLIENT_CUSTOMMSG_FORBID_NPC_SELECT, objId );
						clearInterval(ForbiddenAreaLogic.getInstance().stepIndex);
						GameData.Role.status	=	PlayerActionFlag.NormalWait;
						}
						}*/
						break;
					}
					//				case ObjType.NPC:
					//					playerMove1( rolePlayer, scenePlayer, clickMonster, objId);
					//					break;
				case ObjType.Door:	
					if(CampBattleLogic.getInstance().isJoinCamp)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("99198"));//阵营战开启中
						return
					}
					playerMove1( rolePlayer, scenePlayer, moveDoorOverFun, scenePlayer.playerObj);
					break;
				case ObjType.DinnerNpc:
					playerMove1( rolePlayer, scenePlayer,
						function():void {
							SystemSender.getInstance().CustomMsg(
								CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_DINNER_NPC_TIPS);
							_model.addEventListener( String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_DINNER), getDinnerTips );
						});
					break;
				case ObjType.SnowDrift:
					if(GameData.SceneType==GameDefine.SceneType_SnowBallFightScene)
					{
						playerMove1( rolePlayer, scenePlayer, clickNpc, objId);
					}
					currentNPC	=	scenePlayer;
					break;
				default :
					ActSee.PostClientStatus(8301,1,1);
					playerMove1( rolePlayer, scenePlayer, clickNpc, objId);
					_oldClickPoint			=	new Point();
					_oldClickPoint.x		=	objId.x;
					_oldClickPoint.y		=	objId.y;
					TopOtherPlayerLogic.getInstance().hide();
					currentNPC	=	scenePlayer;
					break;
			}
		}
		
		private function getDinnerTips(e:CustomMsgEvent):void
		{
			var serverArr:Array = e.pList;
			
			if(serverArr[0] == 5){
				var isJoin:int  = int(serverArr[3]);
			}
			
			//0:可参与 1:已经参加 2:不可参与
			switch(isJoin){
				case 0:
					FamilyDinnerUI.getInstance().showOrHide();
					break;
				case 1:
					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("CocktailStr_18"));
					break;
				case 2:
					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("CocktailStr_19"));
					break;
				default:
					break;
			}
			
			_model.removeEventListener( String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_DINNER), getDinnerTips );
		}
		
		private function clickDoor(objId : Point):void
		{
			SystemSender.getInstance().SelectObjInScene(objId, 1);
		}
		
		private function clickNpc(objId : Point):void
		{
			SystemSender.getInstance().SelectObjInScene(objId, Funcid.Funcid_BeginMenu);
		}
		private function leaveGuild():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEAVE_GUILD_SCENE);
		}
		private function clickMonster(objId : Point):void
		{
			var creatorType:int;
			var fbNpcType:int; 
			
			var obj : SceneObject  = _model.scene.GetObject(objId)
			if (obj == null) 
			{
				return;
			}
			SystemSender.getInstance().SelectObjInScene(objId,1);
		}
		private function playerMove1( rolePlayer : BasePlayerModel, scenePlayer : BasePlayerModel, callBack : Function = null, pList : * = null):void
		{
			SceneManager.getInstance().norSceneView.walkOver();
			
			//判断，如果点击的目标和玩家在20以内，return
			if(Point.distance( rolePlayer.point, scenePlayer.point ) <= _distanceMax)
			{
				if( null == callBack ) return;
				if( null != pList )
				{							
					callBack.call(null, pList);
				}
				else
				{
					callBack.call();
				}
				return;
			}
			
			SitCrossleggedLogic.getInstance().onClickStage();
			
			//随即附近的点
			playerMove( rolePlayer, randomPoint( scenePlayer.point ), callBack, pList);
		}
		
		private var interval : int ;
		public function playerMove( player : BasePlayerModel, clickPoint : Point, callBack : Function = null, pList : * = null):Boolean
		{	
			//--------一醉方休
			if(FamilyDinnerUI.getInstance().created){
				FamilyDinnerUI.getInstance().hide();
			}
			if( ( Point.distance( clickPoint, player.point ) < 20 ))
			{//距离过近，不走了
				return false;
			}
			if(	TopOtherPlayerLogic.getInstance().ui.created)
			{
				TopOtherPlayerLogic.getInstance().hide();
			}
			var walkPoint : Point = WalkManager.getInstance().findWayLogic.getWalkNode( clickPoint );
			
			if( WalkManager.getInstance().findWayLogic.isWalk( walkPoint ) )
			{
				//				setTimeout(movetime,1000,player,walkPoint)
				//				function movetime(play:BasePlayerModel,wpoint:Point):void
				//				{
				//告诉服务端我走了
				var newTime:int = getTimer();
				if( newTime - interval > 1000 )
				{
					interval = getTimer();
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PLAYER_MOVETO,
						player.point.x, player.point.y, int(walkPoint.x), int(walkPoint.y));
				}
			
				//				}
				
				//自己在走
				WalkManager.getInstance().roleMove( player, new Point( player.point.x, player.point.y ), walkPoint, callBack, pList );
				
				return true;
			}
			return false;
		}
		public function playerMoveTo( player : BasePlayerModel, clickPoint : Point, callBack : Function = null, pList : * = null):Boolean
		{	
			//--------一醉方休
			if(FamilyDinnerUI.getInstance().created){
				FamilyDinnerUI.getInstance().hide();
			}
			if( ( Point.distance( clickPoint, player.point ) < 10 ))
			{//距离过近，不走了
				return false;
			}
			
			TopOtherPlayerLogic.getInstance().hide();
			var walkPoint : Point = WalkManager.getInstance().findWayLogic.getWalkNode( clickPoint );
			
			if( WalkManager.getInstance().findWayLogic.isWalk( walkPoint ) )
			{
				//告诉服务端我走了
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PLAYER_MOVETO, player.point.x, player.point.y, int(walkPoint.x), int(walkPoint.y));
				
				//自己在走
				WalkManager.getInstance().roleMove( player, new Point( player.point.x, player.point.y ), walkPoint, callBack, pList );
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LEAVE_GUILD_SCENE);
				return true;
			}
			return false;
		}
		/**
		 *  再次点击NPC
		 */		
		public function agaicClickNpc():void
		{
			if(_oldClickPoint != null)
			{
				SystemSender.getInstance().SelectObjInScene(_oldClickPoint, Funcid.Funcid_BeginMenu);
			}
		}
		/**
		 *清处NPC对应点的数据 
		 */		
		public function clearClickNpc():void
		{
			_oldClickPoint	=	null;
		}
		/**
		 * 移到结束后的回调函数
		 */
		private function moveDoorOverFun(obj:BaseObject):void
		{
			if ( null != obj && obj.CheckProperties("EnterFB")) 
			{
				
				var FBListID:int			=	obj.GetProperties("EnterFB");
				var type:int				=	obj.GetProperties("DoorType");
				if(type == 1)
				{
					DuplicatesLogic.getInstance().showInfo("FB_" + FBListID);
				}
				else if(type == 2)
				{
					DuplicatesLogic.getInstance().showInfo("JY_" + FBListID);
				}
				DuplicatesLogic.getInstance().isOpen = false;
			}
		}
		
		private function randomPoint( point : Point ):Point
		{
			var p : Point = point.clone();
			
			point.x += int((_distanceMin + Math.random() * ( _distanceMax - _distanceMin)) * getTimer() % 2 ? 1 : -1);
			point.y += int(_distanceMin + Math.random() * ( _distanceMax - _distanceMin));
			
			if( WalkManager.getInstance().findWayLogic.isWalk( point ))
			{
				return point;
			}
			return p;
		}
	}
}